var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()

Page({
  data: {
    id: 0, //
    info: null, // 用户数据
    defectArr: [{ type: 0, text: '设备类' }, { type: 1, text: '非设备类' },], //  设备类型
    dealArr: [{ type: 0, text: '需要安排处理' }, { type: 1, text: '直接消缺' },], //  缺陷安排处理 0需要安排处理；2直接消缺
    defectStyle: 0, // 配置参数
    invalidReason:'',//作废原因
    isLose: false, // 作废弹框
  },
  onLoad: function (options) {
    if ('id' in options){
      this.setData({
        id: options.id,
        ticket_num: options.number ,
      })
    }
    this._initData()
  },
  onShow: function () { 

  },
  _initData(){
    api.$get(api.defect.info, {id: this.data.id,ticket_num: this.data.ticket_num ,}).then((res) => {
      this.setData({
        info: res.data
      })
    })
  },
  openLoseHandle() { // 作废弹窗
    this.setData({ isLose: true })
  },
  setZuoFei(){//作废
    if($.isNull(this.data.invalidReason)){
      $.toast("作废原因不能为空")
        return
    }
    var params = {
      "id": this.data.id,
      "state":'已作废',//状态变为 ：已作废 
      "invalidReason":this.data.invalidReason,//作废原因
      "invalidPersonid":wx.getStorageSync('user_id'),//作废人id
      "invalidPerson":wx.getStorageSync('user_name'),//作废人
      "cancel":'1',
    }
    api.$post(api.defect.setZuoFei, params).then((res) => {
      $.toast('作废成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },
  cancelZuoFei(){//取消作废
    var params = {
      "id": this.data.id,
      "state":'待安排',//
      "submit":'0',
      "cancel":'0',
    }
    api.$post(api.defect.cancelZuoFei, params).then((res) => {
      $.toast('取消作废成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },
  finishDefect(){
      api.$post(api.defect.finishDefect,{"id":this.data.id}).then((res)=>{
           if(res.data!=null){
             $.toast(res.data);
             if(res.data=='缺陷状态更新成功'){
              setTimeout(()=>{
                $.linkurl('', 'back')
             },1500);
             }

           }else{
              $.toast('缺陷状态更新失败');
           }
      })
  }
})